<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>劳动合同分析API测试</title>
    <style>
        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 {
            color: #333;
            text-align: center;
            margin-bottom: 30px;
        }
        .upload-area {
            border: 2px dashed #ddd;
            border-radius: 10px;
            padding: 40px;
            text-align: center;
            margin-bottom: 20px;
            transition: border-color 0.3s;
        }
        .upload-area:hover {
            border-color: #007bff;
        }
        .upload-area.dragover {
            border-color: #007bff;
            background-color: #f8f9fa;
        }
        input[type="file"] {
            display: none;
        }
        .upload-btn {
            background: #007bff;
            color: white;
            padding: 12px 24px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
            margin: 10px;
        }
        .upload-btn:hover {
            background: #0056b3;
        }
        .analyze-btn {
            background: #28a745;
            color: white;
            padding: 12px 24px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
            width: 100%;
            margin-top: 20px;
        }
        .analyze-btn:hover {
            background: #218838;
        }
        .analyze-btn:disabled {
            background: #6c757d;
            cursor: not-allowed;
        }
        .file-info {
            background: #e9ecef;
            padding: 15px;
            border-radius: 5px;
            margin: 10px 0;
        }
        .result {
            margin-top: 30px;
            padding: 20px;
            border-radius: 5px;
        }
        .result.success {
            background: #d4edda;
            border: 1px solid #c3e6cb;
            color: #155724;
        }
        .result.error {
            background: #f8d7da;
            border: 1px solid #f5c6cb;
            color: #721c24;
        }
        .loading {
            text-align: center;
            padding: 20px;
        }
        .spinner {
            border: 4px solid #f3f3f3;
            border-top: 4px solid #3498db;
            border-radius: 50%;
            width: 40px;
            height: 40px;
            animation: spin 2s linear infinite;
            margin: 0 auto;
        }
        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
        .result-item {
            margin: 10px 0;
            padding: 10px;
            background: #f8f9fa;
            border-left: 4px solid #007bff;
        }
        .result-label {
            font-weight: bold;
            color: #495057;
        }
        .supported-formats {
            background: #fff3cd;
            border: 1px solid #ffeaa7;
            padding: 15px;
            border-radius: 5px;
            margin-bottom: 20px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>🔍 劳动合同分析API测试</h1>
        
        <div class="supported-formats">
            <strong>📋 支持的文件格式：</strong>
            <ul>
                <li>PDF文件 (.pdf)</li>
                <li>Word文档 (.docx)</li>
                <li>图片文件 (.jpg, .png, .bmp, .tiff等)</li>
                <li>ZIP压缩包 (.zip) - 包含多张图片</li>
            </ul>
        </div>

        <div class="upload-area" id="uploadArea">
            <p>📁 点击选择文件或拖拽文件到此处</p>
            <button class="upload-btn" onclick="document.getElementById('fileInput').click()">
                选择文件
            </button>
            <input type="file" id="fileInput" accept=".pdf,.docx,.jpg,.jpeg,.png,.bmp,.tiff,.webp,.zip">
        </div>

        <div id="fileInfo" style="display: none;"></div>
        
        <button class="analyze-btn" id="analyzeBtn" onclick="analyzeContract()" disabled>
            🚀 开始分析合同
        </button>

        <div id="loading" class="loading" style="display: none;">
            <div class="spinner"></div>
            <p>正在分析合同，请稍候...</p>
        </div>

        <div id="result"></div>
    </div>

    <script>
        let selectedFile = null;

        // 文件选择处理
        document.getElementById('fileInput').addEventListener('change', function(e) {
            handleFileSelect(e.target.files[0]);
        });

        // 拖拽上传处理
        const uploadArea = document.getElementById('uploadArea');
        
        uploadArea.addEventListener('dragover', function(e) {
            e.preventDefault();
            uploadArea.classList.add('dragover');
        });

        uploadArea.addEventListener('dragleave', function(e) {
            e.preventDefault();
            uploadArea.classList.remove('dragover');
        });

        uploadArea.addEventListener('drop', function(e) {
            e.preventDefault();
            uploadArea.classList.remove('dragover');
            handleFileSelect(e.dataTransfer.files[0]);
        });

        function handleFileSelect(file) {
            if (!file) return;
            
            selectedFile = file;
            const fileInfo = document.getElementById('fileInfo');
            const analyzeBtn = document.getElementById('analyzeBtn');
            
            fileInfo.innerHTML = `
                <div class="file-info">
                    <strong>📄 已选择文件：</strong>${file.name}<br>
                    <strong>📊 文件大小：</strong>${(file.size / 1024 / 1024).toFixed(2)} MB<br>
                    <strong>🏷️ 文件类型：</strong>${file.type || '未知'}
                </div>
            `;
            fileInfo.style.display = 'block';
            analyzeBtn.disabled = false;
        }

        async function analyzeContract() {
            if (!selectedFile) {
                alert('请先选择文件！');
                return;
            }

            const loading = document.getElementById('loading');
            const result = document.getElementById('result');
            const analyzeBtn = document.getElementById('analyzeBtn');
            
            // 显示加载状态
            loading.style.display = 'block';
            result.innerHTML = '';
            analyzeBtn.disabled = true;

            try {
                const formData = new FormData();
                formData.append('file', selectedFile);

                const response = await fetch('http://localhost:8000/analyze_contract', {
                    method: 'POST',
                    body: formData
                });

                loading.style.display = 'none';

                if (response.ok) {
                    const data = await response.json();
                    displayResult(data, true);
                } else {
                    const errorData = await response.json();
                    displayResult(errorData, false);
                }
            } catch (error) {
                loading.style.display = 'none';
                displayResult({detail: `网络错误: ${error.message}`}, false);
            } finally {
                analyzeBtn.disabled = false;
            }
        }

        function displayResult(data, isSuccess) {
            const result = document.getElementById('result');
            
            if (isSuccess) {
                result.className = 'result success';
                result.innerHTML = `
                    <h3>✅ 分析结果</h3>
                    <div class="result-item">
                        <span class="result-label">文件类型：</span>${data.文件类型}
                    </div>
                    <div class="result-item">
                        <span class="result-label">主体公司名称：</span>${data.主体公司名称}
                    </div>
                    <div class="result-item">
                        <span class="result-label">合同起始日期：</span>${data.合同起始日期}
                    </div>
                    <div class="result-item">
                        <span class="result-label">合同有效期：</span>${data.合同有效期}
                    </div>
                    <div class="result-item">
                        <span class="result-label">约定薪资：</span>${data.约定薪资}
                    </div>
                    <div class="result-item">
                        <span class="result-label">关键信息摘要：</span>${data.关键信息摘要}
                    </div>
                    <div class="result-item">
                        <span class="result-label">文件有效性说明：</span>${data.文件有效性说明}
                    </div>
                    <div class="result-item">
                        <span class="result-label">与案件关联性分析：</span>${data.与案件关联性分析}
                    </div>
                    <div class="result-item">
                        <span class="result-label">是否可以作为核心证据：</span>${data.是否可以作为核心证据}
                    </div>
                `;
            } else {
                result.className = 'result error';
                result.innerHTML = `
                    <h3>❌ 分析失败</h3>
                    <p><strong>错误信息：</strong>${data.detail || '未知错误'}</p>
                `;
            }
        }
    </script>
</body>
</html>